class ColumnTemplate {
  constructor(sheet, number) {
    this.number = number;
    this.sheet = sheet;
    this.style = null;
    // <col min="" ... />
    this.colTag = '';
    this.key = this.number - 1;
  }

  set defn(val) {
    if (val.key) {
      this.key = val.key;
    }
    if (val.style) {
      this.style = val.style;
    }
    if (val.header) {
      const { sheet } = this;
      if (!sheet.rows.length) {
        sheet.addRow();
      }
    }
    if (val.width) {
      this.width = val.width;
    }
  }

  preRender(ctx) {
    const styleId = ctx.styles.addStyleModel(this.style);
    let xml = `<col min="${this.number}" max="${this.number}" customWidth="1" width="${this.width}"`;

    if (styleId) {
      xml += ` style="${styleId}"`;
    }
    if (this.hidden) {
      xml += ' hidden="1"';
    }
    if (this.bestFit) {
      xml += ' bestFit="1"';
    }
    if (this.outlineLevel) {
      xml += ` outlineLevel="${this.outlineLevel}"`;
    }
    if (this.collapsed) {
      xml += ' collapsed="1"';
    }

    this.colTag = xml + '/>';
  }

  clear() {
    delete this.style;
    delete this.sheet;
    delete this.number;
    delete this.hidden;
    delete this.bestFit;
    delete this.outlineLevel;
    delete this.collapsed;
  }

  render(ctx, xmlStream) {
    xmlStream.writeXml(this.colTag);
  }

  getCell(rowNo) {
    return this.sheet.getCell(rowNo, this.number);
  }
}

// 默认列宽
ColumnTemplate.prototype.width = 9;

module.exports = ColumnTemplate;
